//
//  ShichenYijiViewController.m
//  CangLongWnnianli
//
//  Created by Assistant on 2024/12/19.
//

#import "ShichenYijiViewController.h"
#import <Masonry/Masonry.h>

@interface ShichenYijiViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray<NSDictionary *> *shichenData;

@end

@implementation ShichenYijiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self processShichenData];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor colorWithRed:0.941 green:0.949 blue:0.961 alpha:1.0];
    
    // 头部导航
    self.headerView = [[UIView alloc] init];
    self.headerView.backgroundColor = [UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0];
    [self.view addSubview:self.headerView];
    
    // 返回按钮
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backButton setTitle:@"←" forState:UIControlStateNormal];
    [self.backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.backButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [self.backButton addTarget:self action:@selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:self.backButton];
    
    // 标题
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = @"时辰宜忌";
    self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.headerView addSubview:self.titleLabel];
    
    // 表格视图
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ShichenCell"];
    [self.view addSubview:self.tableView];
    
    [self setupConstraints];
}

- (void)setupConstraints {
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(88);
    }];
    
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerView).offset(16);
        make.top.equalTo(self.headerView.mas_top).offset(40);
        make.width.height.mas_equalTo(44);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_top).offset(55);
        make.centerX.equalTo(self.headerView);
        make.height.mas_equalTo(20);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
}

- (void)processShichenData {
    NSMutableArray *processedData = [[NSMutableArray alloc] init];
    
    if (self.huangliData && self.huangliData.shichen_yiji) {
        for (NSDictionary *shichenInfo in self.huangliData.shichen_yiji) {
            [processedData addObject:shichenInfo];
        }
    }
    
    self.shichenData = processedData;
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.shichenData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShichenCell" forIndexPath:indexPath];
    
    // 清除之前的子视图
    for (UIView *subview in cell.contentView.subviews) {
        [subview removeFromSuperview];
    }
    
    NSDictionary *shichenInfo = self.shichenData[indexPath.row];
    BOOL isJi = [shichenInfo[@"jixiong"] isEqualToString:@"吉"];
    
    // 创建卡片容器
    UIView *cardView = [[UIView alloc] init];
    cardView.backgroundColor = [UIColor whiteColor];
    cardView.layer.cornerRadius = 12;
    cardView.layer.shadowColor = [UIColor blackColor].CGColor;
    cardView.layer.shadowOffset = CGSizeMake(0, 2);
    cardView.layer.shadowOpacity = 0.1;
    cardView.layer.shadowRadius = 4;
    [cell.contentView addSubview:cardView];
    
    // 左侧圆圈
    UIView *circleView = [[UIView alloc] init];
    circleView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
    circleView.layer.cornerRadius = 25;
    circleView.layer.borderWidth = 2;
    circleView.layer.borderColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0].CGColor;
    [cardView addSubview:circleView];
    
    UILabel *circleLabel = [[UILabel alloc] init];
    circleLabel.text = shichenInfo[@"dizhi"];
    circleLabel.font = [UIFont boldSystemFontOfSize:18];
    circleLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    circleLabel.textAlignment = NSTextAlignmentCenter;
    [circleView addSubview:circleLabel];
    
    // 时辰名称和时间
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.text = [NSString stringWithFormat:@"%@ %@", shichenInfo[@"ganzhi"], shichenInfo[@"timeRange"]];
    timeLabel.font = [UIFont boldSystemFontOfSize:16];
    timeLabel.textColor = [UIColor blackColor];
    [cardView addSubview:timeLabel];
    
    // 冲煞信息
    UILabel *chongshaLabel = [[UILabel alloc] init];
    chongshaLabel.text = shichenInfo[@"chongsha"];
    chongshaLabel.font = [UIFont systemFontOfSize:14];
    chongshaLabel.textColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
    [cardView addSubview:chongshaLabel];
    
    // 财喜方位
    UILabel *caixiLabel = [[UILabel alloc] init];
    caixiLabel.text = shichenInfo[@"caixi"];
    caixiLabel.font = [UIFont systemFontOfSize:12];
    caixiLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
    [cardView addSubview:caixiLabel];
    
    // 宜标签容器
    UIView *yiContainer = [[UIView alloc] init];
    [cardView addSubview:yiContainer];
    
    // 宜标签
    UIView *yiTagView = [[UIView alloc] init];
    yiTagView.backgroundColor = [UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0];
    yiTagView.layer.cornerRadius = 10;
    [yiContainer addSubview:yiTagView];
    
    UILabel *yiTagLabel = [[UILabel alloc] init];
    yiTagLabel.text = @"宜";
    yiTagLabel.font = [UIFont boldSystemFontOfSize:12];
    yiTagLabel.textColor = [UIColor whiteColor];
    yiTagLabel.textAlignment = NSTextAlignmentCenter;
    [yiTagView addSubview:yiTagLabel];
    
    // 宜内容标签
    UILabel *yiContentLabel = [[UILabel alloc] init];
    NSArray *yiArray = shichenInfo[@"yi"];
    yiContentLabel.text = [yiArray componentsJoinedByString:@" "];
    yiContentLabel.font = [UIFont systemFontOfSize:12];
    yiContentLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    yiContentLabel.numberOfLines = 0;
    [yiContainer addSubview:yiContentLabel];
    
    // 忌标签容器
    UIView *jiContainer = [[UIView alloc] init];
    [cardView addSubview:jiContainer];
    
    // 忌标签
    UIView *jiTagView = [[UIView alloc] init];
    jiTagView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
    jiTagView.layer.cornerRadius = 10;
    [jiContainer addSubview:jiTagView];
    
    UILabel *jiTagLabel = [[UILabel alloc] init];
    jiTagLabel.text = @"忌";
    jiTagLabel.font = [UIFont boldSystemFontOfSize:12];
    jiTagLabel.textColor = [UIColor whiteColor];
    jiTagLabel.textAlignment = NSTextAlignmentCenter;
    [jiTagView addSubview:jiTagLabel];
    
    // 忌内容标签
    UILabel *jiContentLabel = [[UILabel alloc] init];
    NSArray *jiArray = shichenInfo[@"ji"];
    jiContentLabel.text = [jiArray componentsJoinedByString:@" "];
    jiContentLabel.font = [UIFont systemFontOfSize:12];
    jiContentLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    jiContentLabel.numberOfLines = 0;
    [jiContainer addSubview:jiContentLabel];
    
    // 右上角吉凶状态图标
    UIView *statusIconView = [[UIView alloc] init];
    statusIconView.backgroundColor = isJi ? [UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0] : [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
    statusIconView.layer.cornerRadius = 15;
    [cardView addSubview:statusIconView];
    
    UILabel *statusIconLabel = [[UILabel alloc] init];
    statusIconLabel.text = shichenInfo[@"jixiong"];
    statusIconLabel.font = [UIFont boldSystemFontOfSize:12];
    statusIconLabel.textColor = [UIColor whiteColor];
    statusIconLabel.textAlignment = NSTextAlignmentCenter;
    [statusIconView addSubview:statusIconLabel];
    
    // 设置约束
    [cardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(cell.contentView).insets(UIEdgeInsetsMake(8, 16, 8, 16));
        make.height.mas_equalTo(160);
    }];
    
    [circleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cardView).offset(16);
        make.top.equalTo(cardView).offset(16);
        make.width.height.mas_equalTo(50);
    }];
    
    [circleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(circleView);
    }];
    
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(circleView.mas_right).offset(12);
        make.top.equalTo(cardView).offset(16);
    }];
    
    [chongshaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(circleView.mas_right).offset(12);
        make.top.equalTo(timeLabel.mas_bottom).offset(4);
    }];
    
    [caixiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(circleView.mas_right).offset(12);
        make.top.equalTo(chongshaLabel.mas_bottom).offset(4);
        make.right.equalTo(cardView).offset(-60);
    }];
    
    [statusIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(cardView).offset(-16);
        make.top.equalTo(cardView).offset(16);
        make.width.height.mas_equalTo(30);
    }];
    
    [statusIconLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(statusIconView);
    }];
    
    [yiContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cardView).offset(16);
        make.top.equalTo(circleView.mas_bottom).offset(12);
        make.right.equalTo(cardView).offset(-16);
    }];
    
    [yiTagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(yiContainer);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    [yiTagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(yiTagView);
    }];
    
    [yiContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(yiTagView.mas_right).offset(8);
        make.top.right.bottom.equalTo(yiContainer);
    }];
    
    [jiContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cardView).offset(16);
        make.top.equalTo(yiContainer.mas_bottom).offset(8);
        make.right.equalTo(cardView).offset(-16);
        make.bottom.equalTo(cardView).offset(-16);
    }];
    
    [jiTagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(jiContainer);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    [jiTagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(jiTagView);
    }];
    
    [jiContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(jiTagView.mas_right).offset(8);
        make.top.right.bottom.equalTo(jiContainer);
    }];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 184; // 160 + 24 (上下边距)
}

#pragma mark - Actions

- (void)backButtonTapped {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
